package com._dev.social.controller;

import com._dev.social.common.BaseResponse;
import com._dev.social.common.ErrorCode;
import com._dev.social.common.ResultUtils;
import com._dev.social.mode.domain.Threads;
import com._dev.social.mode.domain.response.ThreadResp;
import com._dev.social.service.ThreadsService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @program: social_median_backend
 * @description:
 * @author: Mr.Liu
 * @create: 2024-11-06 17:55
 **/

@RestController
@RequestMapping("/api/thread")
public class ThreadController {
    @Autowired
    private ThreadsService threadsService;

    @PostMapping("/add")
    public BaseResponse<String> addThread(@RequestBody Threads threads) {
        if (Objects.isNull(threads)) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        }
        threadsService.addThread(threads);
        return ResultUtils.succeed("添加成功");
    }

    @PostMapping("/listById")
    public BaseResponse<List<Threads>> getThreadById(String uid) {
        if (uid.isEmpty()) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.succeed(threadsService.getThreadById(uid));
    }

    @GetMapping("/list")
    public BaseResponse<List<ThreadResp>> getAllThreads() {
        return ResultUtils.succeed(threadsService.getThreadsWithUserInfoOptimized());
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteThread(String userId, int tid) {
        if (userId.isEmpty() || tid < 1) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.succeed(threadsService.deleteThread(userId, tid));
    }

    @PostMapping("/listByTid")
    public BaseResponse<Threads> getThreadByTid(String tid) {
        if (tid.isEmpty()) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.succeed(threadsService.lambdaQuery().eq(Threads::getTid, tid).one());
    }

    @PostMapping("/listFollowThreadById")
    public BaseResponse<List<ThreadResp>> getListFollowThreadById(String userId) {
    if (Strings.isEmpty(userId)) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.succeed(threadsService.getListFollowThreadById(userId));
    }
}
